package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.request.Actions;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

public class DownloadCommand extends UserCommandBase implements Command {

	public DownloadCommand(ClientRequest req) {
		super(req);
	}

	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		String filename = req.getFileName();

		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(filename, "Filename is not provided.");

		userService.actionAllowed(username, Actions.DOWNLOAD);
		log.info("User " + username + "requested to download " + filename);

		byte[] decFile = fileService.downloadFile(req);

		ServerResponse rep = new ServerResponse();
		rep.setSuccess(true);
		rep.setMessage("File downloaded.");
		rep.setFileBytes(decFile);
		rep.setFilename(filename);

		log.info("Download action successful for file " + filename);
		return rep;
	}
}